import 'dart:async';

import 'package:game/pages/game_page.dart';
import 'package:game/pages/hall_page.dart';
import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
  final String title;

  MyHomePage({this.title});

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _currentIndex = 0;

  PageController _pageController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _pageController = PageController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: PageView(
        onPageChanged: _onTap,
        controller: _pageController,
        children: <Widget>[
          GamePage(),
          HallPage(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        fixedColor: Colors.blueAccent,
        items: [
          BottomNavigationBarItem(
              icon: Icon(Icons.assistant_photo), title: Text("游戏")),
          BottomNavigationBarItem(icon: Icon(Icons.sort), title: Text("排行")),
        ],
        onTap: _onTap,
        currentIndex: _currentIndex,
      ),
    );
  }

  void _onTap(int value) {
    setState(() {
      _currentIndex = value;
      _pageController.animateToPage(value,
          duration: Duration(milliseconds: 1), curve: Curves.ease);
    });
  }
}

class TouZi extends StatelessWidget {
  final int number;

  TouZi({this.number});

  @override
  Widget build(BuildContext context) {
    Widget widget = _selectWidget(number);
    return Container(
      width: 90.0,
      height: 90.0,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.0),
        color: Colors.white,
      ),
      child: Center(
        child: widget,
      ),
    );
  }

  Widget _selectWidget(int number) {
    final Color color =
        number == 1 || number == 4 ? Colors.redAccent : Colors.blueAccent;
    final double radius = number == 1 ? 15.0 : 10.0;
    final widget = CircleAvatar(
      radius: radius,
      backgroundColor: color,
    );
    switch (number) {
      case 1:
        return widget;

      case 2:
        return Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            widget,
            widget,
          ],
        );

      case 3:
        return Column(
          children: <Widget>[
            const SizedBox(
              height: 10.0,
            ),
            Row(
              children: <Widget>[
                const SizedBox(
                  width: 10.0,
                ),
                widget,
              ],
              mainAxisAlignment: MainAxisAlignment.start,
            ),
            widget,
            Row(
              children: <Widget>[
                widget,
                const SizedBox(
                  width: 10.0,
                ),
              ],
              mainAxisAlignment: MainAxisAlignment.end,
            ),
            const SizedBox(
              height: 10.0,
            ),
          ],
        );

      case 4:
        return Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            Row(
              children: <Widget>[widget, widget],
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            ),
            Row(
              children: <Widget>[widget, widget],
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            ),
          ],
        );

      case 5:
        return Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            Row(
              children: <Widget>[widget, widget],
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            ),
            Center(
              child: widget,
            ),
            Row(
              children: <Widget>[widget, widget],
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            ),
          ],
        );

      case 6:
        return Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            Row(
              children: <Widget>[widget, widget],
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            ),
            Row(
              children: <Widget>[widget, widget],
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            ),
            Row(
              children: <Widget>[widget, widget],
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            ),
          ],
        );
    }
  }
}
